package view.popup 
{
	import com.greensock.TweenMax;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import model.GameData;
	import view.Base;
	
	/**
	 * ...
	 * @author @Boy_Kute
	 */
	public class PopupResultBetting extends Base 
	{
		private var _arrBet:Array;
		private var _maskResult:Sprite;
		private var _container:Sprite;
		private var _mask:Sprite;
		private var _countCheck:int = 0;
		private var _count:int = 5;
		private var _total:int;
		private var _locX:Number = 55;
		
		public function PopupResultBetting() 
		{
			addContent("control_userBetting", "playGame");
			
			_maskResult = new Sprite();
			_maskResult.graphics.beginFill(0x0000FF);
			_maskResult.graphics.drawRect(0, 0, 427, 183)
			addChild(_maskResult);
			
			content.mask = _maskResult;
			
			content.y = 183;
			
			_container = new Sprite();
			_container.graphics.beginFill(0x0000FF, 0);
			_container.graphics.drawRect(0, 0, 322, 82)
			content.addChild(_container);
			_container.x = 55;
			_container.y = 10;
			
			_mask = new Sprite();
			_mask.graphics.beginFill(0x0000FF, 0);
			_mask.graphics.drawRect(0, 0, 322, 82)
			content.addChild(_mask);
			_mask.x = 55;
			_mask.y = 10;
			_container.mask = _mask;
			
			GameData.getInstance().addEventListener("updateBetting", onUpdateBetting);
			
			content.btn_pre.addEventListener(MouseEvent.CLICK, onBack);
			content.btn_next.addEventListener(MouseEvent.CLICK, onNext);
		}
		
		private function onBack(e:MouseEvent):void 
		{
			content.btn_pre.removeEventListener(MouseEvent.CLICK, onBack);
			content.btn_next.removeEventListener(MouseEvent.CLICK, onNext);
			if (_count > 5 && _total > 5) //
			{
				_count--;
				TweenMax.to(_container, .5, { x:_locX + 65, onComplete:onComplete } );
			}
			else 
			{
				content.btn_pre.addEventListener(MouseEvent.CLICK, onBack);
				content.btn_next.addEventListener(MouseEvent.CLICK, onNext);
			}
			
		}
		
		private function onComplete():void 
		{
			_locX = _container.x;
			
			content.btn_pre.addEventListener(MouseEvent.CLICK, onBack);
			content.btn_next.addEventListener(MouseEvent.CLICK, onNext);
			
		}
		
		private function onNext(e:MouseEvent):void 
		{
			content.btn_pre.removeEventListener(MouseEvent.CLICK, onBack);
			content.btn_next.removeEventListener(MouseEvent.CLICK, onNext);
			
			if (_count < _total && _total > 5) //
			{
				_count++;
				TweenMax.to(_container, .5, { x:_locX - 65, onComplete:onComplete } );
			}
			else 
			{
				content.btn_pre.addEventListener(MouseEvent.CLICK, onBack);
				content.btn_next.addEventListener(MouseEvent.CLICK, onNext);
			}
			
			
		}
		
		public function removeAllEvent():void 
		{
			GameData.getInstance().removeEventListener("updateBetting", onUpdateBetting);
			content.btn_pre.removeEventListener(MouseEvent.CLICK, onBack);
			content.btn_next.removeEventListener(MouseEvent.CLICK, onNext);
		}
		
		private function onUpdateBetting(e:Event):void 
		{
			_total = 0;
			var check:Boolean = false;
			var i:int;
			if (_arrBet && _arrBet.length > 0) 
			{
				for (i = 0; i < _arrBet.length; i++) 
				{
					_arrBet[i].parent.removeChild(_arrBet[i]);
					_arrBet[i] = null;	
				}
			}
			
			_arrBet = [];
			
			var count:int;
			
			for (i = 0; i < 12; i++) 
			{
				if (GameData.getInstance().totalEachOfFish["fish" + String(i + 1)][1] > 0)
				{
					var animal:MovieClip = getImage("animal_userBetting", "playGame");
					_container.addChild(animal);
					animal.x = 12.5 + 65 * count;
					animal.y = 20;
					
					_arrBet.push(animal);
					count++;
					
					animal.txtHS.text = GameData.getInstance().totalEachOfFish["fish" + String(i + 1)][0];
					animal.txtDat.text = GameData.getInstance().totalEachOfFish["fish" + String(i + 1)][1];
					
					animal.gotoAndStop(i + 1);
					
					check = true;
					trace("thang nay dc ghi he so va tien chua: ", animal.txtHS.text, animal.txtDat.text)
					_total++;
				}
			}
			
			if (check) 
			{
				if (content.y != 0) 
				{
					TweenMax.to(content, 1, { y:0 } );
				}
				
			}
			
		}
		
		
	}

}